﻿$(document).ready(function() {
    SetupDateRangePicker();
});

function SetupDateRangePicker() {
    var pathname = window.location.pathname;
    $("#rangepicker")
        .daterangepicker({
            onSelectRangeChange: function(range) {
                ShowBlock();        
                RedirectToNewUrl(pathname, range);
            }
        });

    $("#rangepicker")
        .mouseover(function() { $(this).addClass("selected"); })
        .mouseout(function() { $(this).removeClass("selected"); });
}

function RedirectToNewUrl(baseUrl, range) {
    var dates = range.replace(/ /gi, "").replace(/[/]/gi, "").split("-");
    var newUrl =
        baseUrl
            .replace(/\d{8}/, "fromdate").replace(/\d{8}/, "todate")
            .replace("fromdate", dates[0])
            .replace("todate", dates[1]);

    window.location = newUrl;
}